<template>
  <div class="passwordContainer">
    <el-dialog
        title="修改密码"
        :visible.sync="dialogPassword"
        width="420px"
        :before-close="closeALL">
          <el-form label-width="80px" size="medium" :rules="rules" :model="password" class="passform">
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input type="password" v-model="password.oldPassword"> </el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input type="password" v-model="password.newPassword"> </el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input type="password" v-model="password.confirmPassword"> </el-input>
            </el-form-item>
          </el-form>
          <p class="tip">{{wrongMessage}}</p>
          <span slot="footer" class="dialog-footer">
            <el-button @click="closeALL">取 消</el-button>
            <el-button type="primary" @click="passwordChangeSure">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import appUtil from '~/assets/utils/appUtil.js';
export default {
    name: 'passwordChange',
    props: ['dialogPassword'],
    data(){
        return {
            password: {
                oldPassword: '',
                newPassword: '',
                confirmPassword: '',
            },
            rules: {
                oldPassword: [
                    { required: true, message: '请输入旧密码', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请输入新密码', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请确认密码', trigger: 'blur' }
                ],
            },
            wrongMessage: '',
        };
    },
    created(){
        
    },
    watch: {
        passwordChangeVisible(newVal){
            if( newVal === true ){
                //清空密码框数据
                this.password = {
                    oldPassword: '',
                    newPassword: '',
                    confirmPassword: '',
                };
            }
        }
    },
    methods:{
        passwordChangeSure(){
            this.wrongMessage = '';
            let pwd = {
                oldPassword: this.password.oldPassword,
                newPassword: this.password.newPassword
            };
            if(this.password.newPassword === '' || this.password.oldPassword === ''){
                this.wrongMessage = '密码不能为空';
                return false;
            }
            if(this.password.newPassword !== this.password.confirmPassword){
                this.wrongMessage = '两次输入的密码不一致';
                return false;
            }
            let params = {
                password: this.password.oldPassword,
                username: appUtil.getSes('userCode')
            };
            this.$axios.post('/crux-uc/api/user/validate',params).then( res => {
                if (!res.data) {
                    this.wrongMessage='原密码错误';
                    return;
                }
                this.$axios.post('/crux-uc/api/user/changepassword',pwd).then( res => {
                    if(res.data === ''){
                        this.$message({
                            message: '密码修改成功',
                            type: 'success',
                        });
                        this.closeALL();
                    } else {
                        this.wrongMessage = '密码修改失败';
                    }
                });
            }).catch( err => {
                console.error(err);
            });
        },
        // 关闭弹框
        closeALL() {
            // 子组件调用父组件的方法
            this.$parent.closeALL();
        }
    }
};
</script>

<style lang="scss">
  .passwordContainer{
     .el-dialog__body{
          padding-bottom: 0px;
      }
     .el-form-item{
          margin-right: 20px;
      }
  }
  .pwd {
    .el-form-item {
      margin-bottom: 30px;
    }
    .el-item{
      margin-bottom: 0;
      
    }
    .el-row {
      margin-bottom: 10px;
    }
  }
</style>
<style scoped>
.tip {
  color: red;
  text-align: center;
  letter-spacing: 1px;
}
.passform{
    margin-top: 16px;
}
</style>